// #include "util.hpp"
// #include "level.hpp"
// #include "message.hpp"
// #include "format.hpp"
// #include "sink.hpp"
// #include "logger.hpp"
// #include "buffer.hpp"
// #include "looper.hpp"
#include "../logs/mylog.h"
#include <unistd.h>

void test_log(const std::string &name) {
    INFO("%s", "测试开始");
    mylog::Logger::ptr logger = mylog::LoggerManger::getInstance().getLogger(name);
    DEBUG("%s", "测试日志");
    INFO("%s", "测试日志");
    WARN("%s", "测试日志");
    ERROR("%s", "测试日志");
    FATAL("%s", "测试日志");
    INFO("%s", "测试结束");
}

int main() {

    // 建造者模式
    std::unique_ptr<mylog::LoggerBuilder> builder(new mylog::GlobalLoggerBuilder());
    builder->buildLoggerName("async_logger");
    builder->buildLoggerLevel(mylog::LogLevel::value::WARN);
    builder->buildFormatter("[%c][%f:%l]%m%n");
    builder->buildLoggerType(mylog::LoggerType::LOGGER_ASYNC);
    builder->buildSink<mylog::StdoutSink>();
    builder->buildSink<mylog::FileSink>("./logfile/ASYNCtest.log");
    builder->buildSink<mylog::RollBySizeSink>("./logfile/ASYNC-roll-", 1024*1024);
    mylog::Logger::ptr logger = builder->build();

    test_log("async_logger");
    // //读取问价数据 一点一点写入缓冲区，最终将缓冲区数据写入文件，判断生成的新文件与源文件是否一致
    // std::ifstream ifs("./logfile/test.log", std::ios::binary);
    // if (ifs.is_open() == false) {return -1;}
    // ifs.seekg(0, std::ios::end); // 读写位置跳转到文件末尾
    // size_t fsize = ifs.tellg(); // 获取当前位置相对于起始位置的偏移量
    // ifs.seekg(0, std::ios::beg); // 重新跳转到起始位置
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0], fsize);
    // if(ifs.good() == false) {std::cout<<"read error\n"; return -1;}
    // ifs.close();

    // mylog::Buffer buffer;
    // for(int i=0; i<body.size(); i++) {
    //     buffer.push(&body[i], 1);
    // }
    // std::ofstream ofs("./logfile/tmp.log", std::ios::binary);

    // size_t rsize = buffer.readAbleSize();
    // for(int i=0; i<rsize; i++) {
    //     ofs.write(buffer.begin(), 1);
    //     buffer.moveReader(1);
    // }

    // std::string logger_name = "sync_logger";
    // mylog::LogLevel::value limit = mylog::LogLevel::value::WARN;
    // mylog::Formatter::ptr fmt(new mylog::Formatter("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n"));
    // mylog::LogSink::ptr stdout_lsp = mylog::SinkFactory::create<mylog::StdoutSink>();
    // mylog::LogSink::ptr file_lsp = mylog::SinkFactory::create<mylog::FileSink>("./logfile/test.log");
    // mylog::LogSink::ptr roll_lsp = mylog::SinkFactory::create<mylog::RollBySizeSink>("./logfile/roll-", 1024*1024);
    // std::vector<mylog::LogSink::ptr> sinks = {stdout_lsp, file_lsp, roll_lsp};
    // mylog::Logger::ptr logger(new mylog::SyncLogger(logger_name, limit, fmt, sinks));


    // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
    // size_t cursize = 0, count=0;
    // // std::string str = "测试日志";
    // while(cursize < 1024*1024*10) {
    //     // std::string tmp = str+std::to_string(count++);
    //     // roll_lsp->log(tmp.c_str(), tmp.size());
    //     logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    //     cursize += 20;
    // }

    // mylog::LogMsg msg(mylog::LogLevel::value::INFO, 6, "main.c", "lyx", "格式化功能测试。。。");
    // mylog::Formatter fmt;
    // std::string str = fmt.format(msg);
    // mylog::LogSink::ptr stdout_lsp = mylog::SinkFactory::create<mylog::StdoutSink>();
    // mylog::LogSink::ptr file_lsp = mylog::SinkFactory::create<mylog::FileSink>("./logfile/test.log");
    // mylog::LogSink::ptr roll_lsp = mylog::SinkFactory::create<mylog::RollBySizeSink>("./logfile/roll-", 1024*1024);
    // stdout_lsp->log(str.c_str(), str.size());
    // file_lsp->log(str.c_str(), str.size());
    // size_t curszie = 0;
    // size_t count = 0;
    // while(curszie < 1024*1024*10) {
    //     std::string tmp = str+std::to_string(count++);
    //     roll_lsp->log(tmp.c_str(), tmp.size());
    //     curszie += tmp.size();
    // }

    // mylog::LogSink::ptr time_lsp = mylog::SinkFactory::create<mylog::RollByTimeSink>("./logfile/roll-", mylog::TimeGap::GAP_SECOND);
    // time_t old = mylog::util::Date::now();
    // while(mylog::util::Date::now() < old+5) {
    //     time_lsp->log(str.c_str(), str.size());
    //     usleep(1000);
    // }
    //std::cout << str << std::endl;
    // std::cout<<mylog::LogLevel::toString(mylog::LogLevel::value::DEBUG)<<std::endl;
    // std::cout<<mylog::LogLevel::toString(mylog::LogLevel::value::INFO)<<std::endl;
    // std::cout<<mylog::LogLevel::toString(mylog::LogLevel::value::WARN)<<std::endl;
    // std::cout<<mylog::LogLevel::toString(mylog::LogLevel::value::ERROR)<<std::endl;
    // std::cout<<mylog::LogLevel::toString(mylog::LogLevel::value::FATAL)<<std::endl;
    // std::cout<<mylog::LogLevel::toString(mylog::LogLevel::value::OFF)<<std::endl;
    // std::cout<<mylog::util::Date::now()<<std::endl;
    // std::string pathname ="./abc/bca/cba";
    // mylog::util::File::creatDirectory(mylog::util::File::path(pathname));
    return 0;
}